﻿Public Class cls_Sales_Update
    Inherits cls_Database_Base
#Region "Get Data"
    Public Function FuncG_GetData() As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT * FROM [TBL_SALES]"
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
    Public Function FuncG_GetData(ByVal iStr_ID As String) As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT TOP 1 * FROM [TBL_SALES] WHERE LOC_ID = @iNVChr_LOCID AND [ID] = @iNVChr_ID"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
    Public Function FuncG_GetDataByTableID(ByVal iInt_TableID As String) As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT TOP 1 * FROM [TBL_SALES] WHERE LOC_ID = @iNVChr_LOCID AND TABLE_ID = @iInt_TABLEID"
                mvStr_Sql += "  AND STATUS <> 0 ORDER BY CREATEDATE DESC"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iInt_TABLEID", iInt_TableID)
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
#End Region
#Region "Create invoice number"
    Public Function FuncG_CreateInvoiceNo() As String
        Dim mvStr_ID As String = ""
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                'Tìm hóa đơn đầu tiên trong ngày, nếu có lấy số HĐ lớn nhất
                mvStr_Sql = "SELECT TOP 1 * FROM [TBL_SALES] WHERE LOC_ID = @iNVChr_LOCID AND DATEADD(day, 0, DATEDIFF(day, 0, CREATEDATE)) = @iDate_CREATEDATE ORDER BY CAST (LEFT(ID,5) AS INT) DESC"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iDate_CREATEDATE", Today.Date)
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
                If mvDataTale.Rows.Count <> 0 Then
                    mvStr_ID = FuncG_CreateSerial(mvDataTale.Rows(0)("ID"), 5)
                Else
                    mvStr_ID = "00001-" + Format(Today, "ddMMyyyy") + gvStr_LocID
                End If
            End If        
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvStr_ID
    End Function
#End Region
#Region "Insert - Update"
    Public Function FuncG_Insert(ByVal iCls_Sales As cls_Sales) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_Sales.ID)
                SubM_AddParameter("@iNVChr_CASHIER", iCls_Sales.CashierID)
                SubM_AddParameter("@iNVChr_CUSTOMER", iCls_Sales.CustomerID)
                SubM_AddParameter("@iNVChr_WAITER", iCls_Sales.WaiterID)
                SubM_AddParameter("@iInt_TABLE", iCls_Sales.TableID)
                SubM_AddParameter("@iDate_CHECKIN", iCls_Sales.CheckIn)
                SubM_AddParameter("@iTInt_VAT", iCls_Sales.VAT)
                SubM_AddParameter("@iTInt_STATUS", iCls_Sales.Status)
                If FuncM_ExecuteStore("usp_InsertSales") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
    Public Function FuncG_UpdateStatus(ByVal iStr_ID As String, ByVal iByte_Status As Byte) As Boolean
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "UPDATE [TBL_SALES] SET STATUS = @iTInt_STATUS, CHECK_OUT = getdate()"
                mvStr_Sql += "  WHERE LOC_ID = @iNVChr_LOCID AND ID = @iNVChr_ID"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                SubM_AddParameter("@iTInt_STATUS", iByte_Status)
                If FuncM_ExecuteNonQuery(mvStr_Sql) Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
    Public Function FuncG_DeleteBill(ByVal iStr_ID As String, ByVal iStr_UserID As String, ByVal iByte_Status As Byte) As Boolean
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "UPDATE [TBL_SALES] SET STATUS = @iTInt_STATUS, WAITER_ID = @iNVChr_UserID, CHECK_OUT = getdate()"
                mvStr_Sql += "  WHERE LOC_ID = @iNVChr_LOCID AND ID = @iNVChr_ID"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                SubM_AddParameter("@iNVChr_UserID", iStr_UserID)
                SubM_AddParameter("@iTInt_STATUS", iByte_Status)
                If FuncM_ExecuteNonQuery(mvStr_Sql) Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
#End Region
#Region "Delete"
    Public Function FuncG_Delete(ByVal iStr_ID As String, ByVal iInt_TableID As Integer) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                SubM_AddParameter("@iInt_TABLE", iInt_TableID)
                If FuncM_ExecuteStore("usp_DeleteSales") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
#End Region
#Region "Transfer table"
    Public Function FuncG_Transfer(ByVal iStr_ID As String, ByVal iInt_TableID1 As Integer, ByVal iInt_TableID2 As Integer) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                SubM_AddParameter("@iInt_TABLE1", iInt_TableID1)
                SubM_AddParameter("@iInt_TABLE2", iInt_TableID2)
                If FuncM_ExecuteStore("usp_TransferTable") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
#End Region
End Class
